package cart;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Discount_code_positive extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled10() throws Exception {
    driver.get(baseUrl + "ottoman.html");
    driver.findElement(By.cssSelector("button.button.btn-cart")).click();
    try {
      assertEquals("Ottoman was added to your shopping cart.", driver.findElement(By.cssSelector("li > span")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.id("coupon_code")).clear();
    driver.findElement(By.id("coupon_code")).sendKeys("qatest");
    driver.findElement(By.cssSelector("div.buttons-set > button.button")).click();
    try {
      assertEquals("Coupon code \"qatest\" was applied.", driver.findElement(By.cssSelector("li.success-msg > ul > li")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertEquals("Discount (qatest)", driver.findElement(By.xpath("//table[@id='shopping-cart-totals-table']/tbody/tr[2]/td")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("Remove item")).click();
    driver.findElement(By.linkText("here")).click();
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
